/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-11-01     hehung       the first version
 */
#include "app_esp8266.h"
#include "app_uart.h"
#include "board.h"
#include "app_oled.h"
#include <string.h>
#include <stddef.h>
#include "app_beep.h"
#include "app_rtc.h"

#define ESP8266_ATE0             ((uint8_t)0u)
#define ESP8266_AT               ((uint8_t)1u)
#define ESP8266_CWMODE           ((uint8_t)2u)
#define ESP8266_CIPMUX           ((uint8_t)3u)
#define ESP8266_CIPMODE          ((uint8_t)4u)
#define ESP8266_CIPSEND          ((uint8_t)5u)
#define ESP8266_CWJAP            ((uint8_t)6u)
#define ESP8266_CIPSTART         ((uint8_t)7u)
#define ESP8266_EXIT_SERIALNET   ((uint8_t)8u)
#define ESP8266_EXIT_SERIAL_WAIT ((uint8_t)9u)
#define ESP8266_DATA_TRANS       ((uint8_t)0x0Au)
#define ESP8266_GET_TIME         ((uint8_t)0x0Bu)
#define ESP8266_IDLE             ((uint8_t)0xF0u)
#define ESP8266_OFF              ((uint8_t)0xF1u)

#define ESP8266_Recv_TIME_CLEAR  ((uint8_t)0u)
#define ESP8266_Recv_TIME_START  ((uint8_t)1u)
#define ESP8266_Recv_TIMEOUT     ((uint8_t)30u)    /*300ms*/
#define ESP8266_Recv_TIMEOUT_1S  ((uint8_t)100u)

#define ESP8266_RE_SEND_CNT_CLR  ((uint8_t)0u)
#define ESP8266_RE_SEND_TIMEOUT  ((uint8_t)100u)

#define ESP8266_EXIT_SERIAL_MODE_WAITING_CNT    ((uint8_t)5)    /*50ms*/

typedef struct
{
    char m_wifi_name[20];
    char m_wifi_password[20];
    char m_ip_addr[18];
    char m_ip_port[8];
} s_wifi_ip;

static uint8_t ESP8266_01_reset_pin = 0;

static uint8_t ESP8266_Command = ESP8266_EXIT_SERIALNET;
static uint8_t ESP8266_WaitingCommand = ESP8266_EXIT_SERIALNET;   /*The command is waiting echo*/
static uint16_t ESP8266_timeout_cnt = ESP8266_Recv_TIME_CLEAR;
static uint8_t ESP8266_ReSendCnt = ESP8266_RE_SEND_CNT_CLR;
static bool ESP8266_Init_Succ_flg = false;
static uint16_t ESP8266_Retry_thd = ESP8266_Recv_TIMEOUT;
static rt_thread_t esp8266_thread;

static uint8_t ESP8266_recv_img_data[OLED_ESP8266_RECV_IMG_NUM] = {0xFF};
static uint8_t ESP8266_recv_img_seq_cnt = 1u;
static uint16_t ESP8266_recv_img_data_cnt = 0u;

static s_wifi_ip ESP8266_current_wifi_ip;

static void ESP8266_ThreadManage(void);
static void ESP8266_DataHandle(const char *paraData);
static void ESP8266_DateUpdate(const char *paraData);

uint8_t* ESP8266_GetRecvMsgData(void)
{
    return ESP8266_recv_img_data;
}

void ESP8266_SysInit(void)
{
    ESP8266_01_reset_pin = rt_pin_get("PA.0");
    rt_pin_mode(ESP8266_01_reset_pin, PIN_MODE_OUTPUT);
    rt_pin_write(ESP8266_01_reset_pin, PIN_LOW);

    /*Default, can be changed by shell command ''*/
    rt_sprintf(ESP8266_current_wifi_ip.m_wifi_name, "%s", "hehung_baymaxv");    /*Wifi name*/
    rt_sprintf(ESP8266_current_wifi_ip.m_wifi_password, "%s", "1234543210");    /*Wifi password*/
    rt_sprintf(ESP8266_current_wifi_ip.m_ip_addr, "%s", "192.168.101.11");       /*server ip address*/
    rt_sprintf(ESP8266_current_wifi_ip.m_ip_port, "%s", "8080");                /*server port*/

    ESP8266_ThreadManage();
}

void ESP8266_SysRetey(void)
{
    ESP8266_Command = ESP8266_EXIT_SERIALNET;
    ESP8266_WaitingCommand = ESP8266_EXIT_SERIALNET;
    rt_pin_write(ESP8266_01_reset_pin, PIN_HIGH);
    rt_pin_write(ESP8266_01_reset_pin, PIN_LOW);
}

void ESP8266_SysDateTimeRetey(void)
{
    ESP8266_Command = ESP8266_GET_TIME;
    ESP8266_WaitingCommand = ESP8266_GET_TIME;
}

void ESP8266_RecvDataProcess(const char *paraData)
{
    char str[20];

    //rt_kprintf("ESP8266.RX:-->%s\n", paraData);
    switch(ESP8266_WaitingCommand)
    {
        case ESP8266_ATE0:
        {
            //if((paraData[0] == 'O') && (paraData[1] == 'K'))
            if(NULL != strstr(paraData, "OK"))
            {
                ESP8266_Command = ESP8266_AT;
                rt_kprintf("ATE0 Setting success\n");
            }
            else
            {
//                rt_kprintf("ATE0 Setting failed\n");
            }
        } break;
        case ESP8266_AT:
        {
            //if((paraData[0] == 'O') && (paraData[1] == 'K'))
            if(NULL != strstr(paraData, "OK"))
            {
                ESP8266_Command = ESP8266_CWMODE;
                rt_kprintf("AT Verify success\n");
            }
            else
            {
//                rt_kprintf("AT Verify failed\n");
            }
        } break;
        case ESP8266_CWMODE:
        {
            //if((paraData[0] == 'O') && (paraData[1] == 'K'))
            if(NULL != strstr(paraData, "OK"))
            {
                ESP8266_Command = ESP8266_CIPMODE;
                rt_kprintf("AT+CWMODE=1 Setting success\n");
            }
            else
            {
//                rt_kprintf("AT+CWMODE=1 Setting failed\n");
            }
        } break;
        case ESP8266_CIPMODE:
        {
            //if((paraData[0] == 'O') && (paraData[1] == 'K'))
            if(NULL != strstr(paraData, "OK"))
            {
                ESP8266_Command = ESP8266_CWJAP;//ESP8266_CIPMUX;
                rt_kprintf("AT+CIPMODE=1 Setting success\n");
            }
            else
            {
//                rt_kprintf("AT+CIPMODE=1 Setting failed\n");
            }
        } break;
        case ESP8266_CIPMUX:
        {
            //if(((paraData[0] == 'O') && (paraData[1] == 'K'))
            //    || (paraData[0] == 'L'))
            if((NULL != strstr(paraData, "OK"))
                || (NULL != strstr(paraData, "L")))
            {
                ESP8266_Command = ESP8266_CWJAP;
                rt_kprintf("AT+CIPMUX=0 Setting success\n");
            }
            else
            {
//                rt_kprintf("AT+CIPMUX=0 Setting failed\n");
            }
        } break;
        case ESP8266_CWJAP:
        {
            //if((paraData[0] == 'O') && (paraData[1] == 'K'))
            if(NULL != strstr(paraData, "OK"))
            {
                ESP8266_Command = ESP8266_CIPSTART;
                rt_kprintf("AT+CWJAP Setting success\n");
            }
            else
            {
//                rt_kprintf("AT+CWJAP Setting failed\n");
            }
        } break;
        case ESP8266_CIPSTART:
        {
            //if(((paraData[0] == 'O') && (paraData[1] == 'K'))
            //       || ((paraData[8] == 'O') && (paraData[9] == 'O'))
            //       || ((paraData[0] == 'A') && (paraData[1] == 'L'))
            //       || ((paraData[0] == 'C') && (paraData[1] == 'O')))
            if((NULL != strstr(paraData, "OK"))
                || (NULL != strstr(paraData, "OO"))
                || (NULL != strstr(paraData, "AL"))
                || (NULL != strstr(paraData, "CO")))
            {
                ESP8266_Command = ESP8266_CIPSEND;
                ESP8266_Init_Succ_flg = true;
                rt_kprintf("AT+CIPSTART Connect success\n");
            }
            else
            {
//                rt_kprintf("AT+CIPSTART Connect failed\n");
            }
        } break;
        case ESP8266_CIPSEND:
        {

        } break;
        case ESP8266_GET_TIME:
        {
            if(NULL != strstr(paraData, "Date:"))
            {
                //rt_kprintf("---->%s\n",strstr(paraData, "Date:"));
                ESP8266_DateUpdate(strstr(paraData, "Date:"));
            }
        } break;
        case ESP8266_DATA_TRANS:
        {
            //if(((paraData[0] == 'S') && (paraData[1] == 'T'))
            //    || ((paraData[0] == '>') && (paraData[1] == 'S') && (paraData[2] == 'T')))
            if(NULL != strstr(paraData, "ST"))
            {
                OLED_SetDisplayFlag(OLED_DISPLAY_NONE);
                rt_sprintf(str, "OK\r\n");    /*Off echo*/
                UART1_SendUart1Data(str, (uint8_t)rt_strlen(str));
            }
            //else if((paraData[0] == 'B') && (paraData[1] == 'M') &&
            //    (paraData[2] == 'P') && (paraData[3] == '-'))
            else if(NULL != strstr(paraData, "BMP-"))
            {
                //if(paraData[38] == 'S')
                {
                    ESP8266_DataHandle(strstr(paraData, "BMP-"));
                }
                //else
                //{
                //    rt_sprintf(str, "FAIL\r\n");    /*Off echo*/
                //    UART1_SendUart1Data(str, (uint8_t)rt_strlen(str));
                //3}
            }
            //else if((paraData[0] == 'E') && (paraData[1] == 'N') && (paraData[0] == 'D'))
            else if(NULL != strstr(paraData, "END"))
            {
                ESP8266_recv_img_seq_cnt = 1;
                ESP8266_recv_img_data_cnt = 0;
            }
            else if(NULL != strstr(paraData, "CLOSE"))
            {
                rt_kprintf("==>%s<\n", strstr(paraData, "CLOSE"));
            }
        } break;
    }
}

static void ESP8266_DataHandle(const char *paraData)
{
    uint8_t i = 0;
    uint8_t seq = 0;
    char str[10];

    //ESP8266_recv_img_seq_cnt ++;
    seq = (uint8_t)(paraData[4]-'0')*10+(paraData[5]-'0');
    //rt_kprintf(">>%d---%d\n", ESP8266_recv_img_seq_cnt, seq);
    rt_kprintf("--------->%s\n", paraData);
    if(seq == ESP8266_recv_img_seq_cnt)
    {
        ESP8266_recv_img_seq_cnt ++;
        for(i=0; i<32u; i++)
        {
            //ESP8266_recv_img_data[ESP8266_recv_img_data_cnt++] = paraData[i+6];
            ESP8266_recv_img_data[ESP8266_recv_img_data_cnt++] = ((paraData[i+6] >= '0') && (paraData[i+6] <= '9')) ?
                                                                 (paraData[i+6] - '0') : (paraData[i+6] - 'A' + 10);
        }
        rt_sprintf(str, "OK\r\n");    /*Off echo*/
        UART1_SendUart1Data(str, (uint8_t)rt_strlen(str));
        //rt_kprintf("%s\n", paraData);
        OLED_DataDownloadRefresh();

        if(ESP8266_recv_img_seq_cnt == (64u+1u))
        {
            /*reset*/
            ESP8266_recv_img_seq_cnt = 1u;
            ESP8266_recv_img_data_cnt = 0;
            Beep_SetBeepWorkingMode(BEEP_MSG_DOWNLOAF_FINISHED);    /*Beep on after download finished*/
            OLED_SetDisplayFlag(OLED_DISPLAY_TIME);
            OLED_ShowImgFromESP8266Server(ESP8266_recv_img_data, 1u);
            OLED_SetMsgTipsFlag(1u);
        }
    }
    else if(ESP8266_recv_img_seq_cnt > seq)
    {
        /*To avoid server retry send data*/
    }
    else if(ESP8266_recv_img_seq_cnt < seq)
    {
        rt_sprintf(str, "ERROR\r\n");    /*Sequence error echo*/
        UART1_SendUart1Data(str, (uint8_t)rt_strlen(str));
    }
    else
    {
        rt_sprintf(str, "FAIL\r\n");    /*Off echo*/
        UART1_SendUart1Data(str, (uint8_t)rt_strlen(str));
    }

}

static void ESP8266_DateUpdate(const char *paraData)
{
    rtc_date tmp_date;
    uint8_t i = 0u;
    uint8_t arr_date[14];

    if((paraData[19] == 'E') && (paraData[20] == 'N') && (paraData[21] == 'D'))
    {
        for(i=5; i<=18; i++)
        {
            arr_date[i-5] = paraData[i] - '0';    /*Convert the ASCII to number*/
        }
        tmp_date.m_year = arr_date[0]*1000 + arr_date[1]*100 + arr_date[2]*10 + arr_date[3];
        tmp_date.m_month = arr_date[4]*10 + arr_date[5];
        tmp_date.m_day = arr_date[6]*10 + arr_date[7];
        tmp_date.m_hour = arr_date[8]*10 + arr_date[9];
        tmp_date.m_min = arr_date[10]*10 + arr_date[11];
        tmp_date.m_sec = arr_date[12]*10 + arr_date[13];
        rt_kprintf("---->%d,%d,%d,%d,%d,%d\n", tmp_date.m_year,
                                               tmp_date.m_month,
                                               tmp_date.m_day,
                                               tmp_date.m_hour,
                                               tmp_date.m_min,
                                               tmp_date.m_sec);
        RTC_DateUpdate(tmp_date);
        if(OLED_GetWifiRetryStatus() == 1u)
        {
            OLED_SetDisplayFlag(OLED_DISPLAY_WIFI_RETRY);
            OLED_ClrWifiRetryStatus();
        }
        else if(OLED_GetDateTimeRetryStatus() == 1u)
        {
            OLED_SetDisplayFlag(OLED_DISPLAY_INTNETTIME);
            OLED_ClrDateTimeRetryStatus();
        }
        else
        {
            OLED_SetDisplayFlag(OLED_DISPLAY_TIME);
        }
        ESP8266_Command = ESP8266_DATA_TRANS;
    }
}

void ESP8266_DataRecvInterrupt(void)
{
    ESP8266_recv_img_seq_cnt = 0;
    ESP8266_recv_img_data_cnt = 0;
}

static void ESP8266_ManageEntry(void *parameter)
{
    char str[50];
    char ch;
    uint8_t exit_serial_waiting_cnt = 0u;

    while(1)
    {
        switch(ESP8266_Command)
        {
            case ESP8266_EXIT_SERIALNET:
            {
                //rt_sprintf(str, "+++");    /*Off echo*/
                //UART1_SendUart1Data(str, (uint8_t)rt_strlen(str));
                rt_pin_write(ESP8266_01_reset_pin, PIN_HIGH);    /*Reset the ESP8266*/
    //                rt_kprintf("%s\n",str);
                ESP8266_WaitingCommand = ESP8266_EXIT_SERIALNET;
                ESP8266_Command = ESP8266_EXIT_SERIAL_WAIT;
                ESP8266_timeout_cnt = ESP8266_Recv_TIME_START;
                ESP8266_Retry_thd = ESP8266_Recv_TIMEOUT;
            } break;
            case ESP8266_EXIT_SERIAL_WAIT:
            {
                if(exit_serial_waiting_cnt < ESP8266_EXIT_SERIAL_MODE_WAITING_CNT)
                {
                    exit_serial_waiting_cnt++;
                }
                else
                {
                    rt_pin_write(ESP8266_01_reset_pin, PIN_LOW);
                    exit_serial_waiting_cnt = 0;
                    ESP8266_WaitingCommand = ESP8266_EXIT_SERIAL_WAIT;
                    ESP8266_Command = ESP8266_ATE0;
                    ESP8266_timeout_cnt = ESP8266_Recv_TIME_START;
                    ESP8266_Retry_thd = ESP8266_Recv_TIMEOUT;
                }
            }
            case ESP8266_ATE0:
            {
                rt_sprintf(str, "ATE0\r\n");    /*Off echo*/
                UART1_SendUart1Data(str, (uint8_t)rt_strlen(str));
//                rt_kprintf("%s\n",str);
                ESP8266_WaitingCommand = ESP8266_ATE0;
                ESP8266_Command = ESP8266_IDLE;
                ESP8266_timeout_cnt = ESP8266_Recv_TIME_START;
                ESP8266_Retry_thd = ESP8266_Recv_TIMEOUT;
            } break;
            case ESP8266_AT:
            {
                rt_sprintf(str, "AT\r\n");    /*AT Test*/
                UART1_SendUart1Data(str, (uint8_t)rt_strlen(str));
//                rt_kprintf("%s\n",str);
                ESP8266_WaitingCommand = ESP8266_AT;
                ESP8266_Command = ESP8266_IDLE;
                ESP8266_timeout_cnt = ESP8266_Recv_TIME_START;
                ESP8266_Retry_thd = ESP8266_Recv_TIMEOUT;
            } break;
            case ESP8266_CWMODE:
            {
                rt_sprintf(str, "AT+CWMODE=1\r\n");    /*Station Mode*/
                UART1_SendUart1Data(str, (uint8_t)rt_strlen(str));
//                rt_kprintf("%s\n",str);
                ESP8266_WaitingCommand = ESP8266_CWMODE;
                ESP8266_Command = ESP8266_IDLE;
                ESP8266_timeout_cnt = ESP8266_Recv_TIME_START;
                ESP8266_Retry_thd = ESP8266_Recv_TIMEOUT;
            } break;
            case ESP8266_CIPMODE:
            {
                rt_sprintf(str, "AT+CIPMODE=1\r\n");
                UART1_SendUart1Data(str, (uint8_t)rt_strlen(str));
//                rt_kprintf("%s\n",str);
                ESP8266_WaitingCommand = ESP8266_CIPMODE;
                ESP8266_Command = ESP8266_IDLE;
                ESP8266_timeout_cnt = ESP8266_Recv_TIME_START;
                ESP8266_Retry_thd = ESP8266_Recv_TIMEOUT;
            } break;
            case ESP8266_CIPMUX:
            {
                rt_sprintf(str, "AT+CIPMUX=0\r\n");
                UART1_SendUart1Data(str, (uint8_t)rt_strlen(str));
//                rt_kprintf("%s\n",str);
                ESP8266_WaitingCommand = ESP8266_CIPMUX;
                ESP8266_Command = ESP8266_IDLE;
                ESP8266_timeout_cnt = ESP8266_Recv_TIME_START;
                ESP8266_Retry_thd = ESP8266_Recv_TIMEOUT;
            } break;
            case ESP8266_CWJAP:
            {
                rt_sprintf(str, "AT+CWJAP=\"%s\",\"%s\"\r\n",
                                ESP8266_current_wifi_ip.m_wifi_name,
                                ESP8266_current_wifi_ip.m_wifi_password);    /*Station Mode*/
                UART1_SendUart1Data(str, (uint8_t)rt_strlen(str));
//                rt_kprintf("%s\n",str);
                ESP8266_WaitingCommand = ESP8266_CWJAP;
                ESP8266_Command = ESP8266_IDLE;
                ESP8266_timeout_cnt = ESP8266_Recv_TIME_START;
                ESP8266_Retry_thd = ESP8266_Recv_TIMEOUT_1S;
            } break;
            case ESP8266_CIPSTART:
            {
                rt_sprintf(str, "AT+CIPSTART=\"TCP\",\"%s\",%s\r\n",
                                ESP8266_current_wifi_ip.m_ip_addr,
                                ESP8266_current_wifi_ip.m_ip_port);    /*Station Mode*/
                UART1_SendUart1Data(str, (uint8_t)rt_strlen(str));
//                rt_kprintf("%s\n",str);
                ESP8266_WaitingCommand = ESP8266_CIPSTART;
                ESP8266_Command = ESP8266_IDLE;
                ESP8266_timeout_cnt = ESP8266_Recv_TIME_START;
                ESP8266_Retry_thd = ESP8266_Recv_TIMEOUT_1S;
            } break;
            case ESP8266_CIPSEND:
            {
                rt_sprintf(str, "AT+CIPSEND\r\n");
                UART1_SendUart1Data(str, (uint8_t)rt_strlen(str));
                ESP8266_WaitingCommand = ESP8266_GET_TIME;
                ESP8266_Command = ESP8266_GET_TIME;
/*                ESP8266_WaitingCommand = ESP8266_CIPSEND;
                ESP8266_Command = ESP8266_IDLE;*/
                //ESP8266_timeout_cnt = ESP8266_Recv_TIME_START;
                //ESP8266_Retry_thd = ESP8266_Recv_TIMEOUT;
            } break;
            case ESP8266_GET_TIME:
            {
                rt_sprintf(str, "GetTime");
                UART1_SendUart1Data(str, (uint8_t)rt_strlen(str));
                ESP8266_WaitingCommand = ESP8266_GET_TIME;
                ESP8266_Command = ESP8266_IDLE;
            } break;
            case ESP8266_IDLE:
            {
/*                if(ESP8266_DATA_TRANS == ESP8266_WaitingCommand)
                {
                    ch = UART1_ReadCharFromFIFO();
                    if(ch == '>')
                    {
                        OLED_SetDisplayFlag(1);
                        ESP8266_WaitingCommand = ESP8266_DATA_TRANS;
                        ESP8266_Command = ESP8266_DATA_TRANS;
                        ESP8266_Init_Succ_flg = true;
                        rt_kprintf("AT+CIPSEND Setting success\n");
                    }
                    else
                    {

                    }
                }
                else
                {

                }*/
                if(ESP8266_timeout_cnt >= ESP8266_Retry_thd)
                {
                    if(ESP8266_ReSendCnt > ESP8266_RE_SEND_TIMEOUT)
                    {
                        ESP8266_ReSendCnt = ESP8266_RE_SEND_CNT_CLR;
                        /*ESP8266 Initialize Failed, Exit*/
//                        rt_thread_delete(esp8266_thread);
                        ESP8266_Command = ESP8266_OFF;
                        ESP8266_Init_Succ_flg = false;
                        rt_kprintf("ESP8266 Initialize Failed!\n");
                        OLED_SetDisplayFlag(1);
                        continue;
                    }
                    ESP8266_ReSendCnt ++;
                    ESP8266_Command = ESP8266_WaitingCommand;   /*Return to re-send command*/
//                    rt_kprintf("ESP8266 Retry \n");
                }
                else
                {
                    ESP8266_timeout_cnt ++;
                }
//                rt_kprintf("ESP8266 IDLE \n");
            } break;
            case ESP8266_DATA_TRANS:
            {
                ESP8266_WaitingCommand = ESP8266_DATA_TRANS;
            } break;
            case ESP8266_OFF:
            {

            } break;
        }
        rt_thread_mdelay(10);
    }
}

bool ESP8266_GetInitSuccFlg(void)
{
    return ESP8266_Init_Succ_flg;
}

static void ESP8266_ThreadManage(void)
{
    esp8266_thread = rt_thread_create("ESP8266 Initialize Thread Manage",     /*线程名字*/
                                   ESP8266_ManageEntry,/*线程入口函数*/
                                   RT_NULL, /*线程入口函数参数*/
                                   1024,    /*线程栈大小*/
                                   2,       /*线程优先级*/
                                   10);     /*线程时间片*/

    if(esp8266_thread != RT_NULL)
    {
//        rt_kprintf("ESP8266 Initialize Thread Created Success!\n");
        rt_thread_startup (esp8266_thread);
    }
    else
    {
        rt_kprintf("ESP8266 Initialize Thread Create Failed!\n");
    }
}

static int ESP_WifiSetting(int argc, char *argv[])
{
    if(argc == 3)
    {
        rt_sprintf(ESP8266_current_wifi_ip.m_wifi_name, "%s", argv[1]);        /*Wifi name*/
        rt_sprintf(ESP8266_current_wifi_ip.m_wifi_password, "%s", argv[2]);    /*Wifi password*/
        rt_kprintf("Wifi名称与密码设置成功\n");
        return RT_EOK;
    }
    else
    {
        rt_kprintf("输入不合法，请重新输入，输入格式:【ESP_WifiSetting Wifi名称 Wifi密码】\n");
        return RT_ERROR;
    }
}
#ifdef FINSH_USING_MSH
MSH_CMD_EXPORT(ESP_WifiSetting, Set wifi name and password);
#endif

static int ESP_IpSetting(int argc, char *argv[])
{
    if(argc == 3)
    {
        rt_sprintf(ESP8266_current_wifi_ip.m_ip_addr, "%s", argv[1]);       /*server ip address*/
        rt_sprintf(ESP8266_current_wifi_ip.m_ip_port, "%s", argv[2]);                /*server port*/
        rt_kprintf("IP地址与端口号设置成功\n");
        return RT_EOK;
    }
    else
    {
        rt_kprintf("输入不合法，请重新输入，输入格式:【ESP_IpSetting IP地址 端口号】\n");
        return RT_ERROR;
    }
}
#ifdef FINSH_USING_MSH
MSH_CMD_EXPORT(ESP_IpSetting, Set Server IP and PORT);
#endif
